package com.wnxy.util;

import org.apache.ibatis.annotations.Mapper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;

public class BeanUtil {

    public static <T>T populate(Class<T> T, Map<String,String[]> map) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {


          T t=T.getDeclaredConstructor().newInstance();

          Field[] fields=T.getDeclaredFields();

          for(Field field:fields){

              field.setAccessible(true);

              String fieldName=field.getName();


              if(map.containsKey(fieldName)){

                  String value= Arrays.toString(map.get(fieldName));

                  value=value.substring(1,value.length()-1);

                  if(field.getType()==Integer.class){

                      Integer  newValue=Integer.parseInt(fieldName);

                       field.set(t,newValue);
                  }
                  else{

                      field.set(t,value);
                  }



              }

          }



         return t;

    }

}
